#Alexander F. Gazmararian
#afg2@princeton.edu

library(tidyverse)
library(modelsummary)
library(margins)
library(kableExtra)
library(sandwich)

#load functions/themes
source("code/fun/book_theme.R")
source("code/fun/savefig.R")
source("code/fun/fix_txt.R")

#load data
g <- readRDS("data/NatQual_August22.rds")

#customize coefficient names
source("code/fun/coefnames4tables.R")

coefnames <- c(
  "transparent_trt" = "Clawback treatment",
  "PartySummaryNeither:transparent_trt" = "Clawback treatment x Independent",
  "PartySummaryRepublican:transparent_trt" = "Clawback treatment x Republican",
  "transparent_trt:trustfed_Scale" = "Clawback treatment x Government trust",
  "green_index:transparent_trt" = "Clawback treatment x Green jobs index",
  "green_index" = "Green jobs index",
  "trustfed_Scale" = "Government trust",
  coefnames
)

#specify base model
f.base <- y ~ age + Female + Black + Hispanic + income5 + CollegeDegree + PartySummary + employfull + green_index + gw_index + trustfed_Scale

# Vote for politician that supports green tax credits ---------------------

# Estimate linear models

#specify model
f.transp <- update(f.base, clawbackvote_sc ~ . + transparent_trt)

#estimate AME
claw <- lm(f.transp, g)
claw.ame <- margins_summary(claw, variables="transparent_trt", vcov = vcovHC(claw, "HC2")) %>% data.frame()
claw.ame

#heterogeneous effects by party
claw.pid <- lm(update(f.transp, ~ . + PartySummary * transparent_trt), g)
claw.pid.ame <- margins_summary(claw.pid, variables="transparent_trt", at=list(PartySummary=c("Neither", "Republican", "Democrat"))) %>% data.frame()

#effects by trust in government
claw.trust <- lm(update(f.transp, ~ . + trustfed_Scale * transparent_trt), g)

#effects by belief in green energy
claw.green <- lm(update(f.transp, ~ . + green_index * transparent_trt), g)

##Create Figure 8.6 ----
#bind together the main AME and the heterogeneous effects by party
claw.pid.ame$model <- "Conditional Average\nTreatment Effect by Party:"
claw.ame$model <- "Average Treatment Effect:"
claw.ame$PartySummary <- "Clawback Treatment"
claw.out <- dplyr::bind_rows(claw.pid.ame, claw.ame)
#calculate 90% confidence intervals
claw.out$lb90 <- with(claw.out, AME + SE * qnorm(0.05))
claw.out$ub90 <- with(claw.out, AME + SE * qnorm(0.95))
#create book plot
clawplot <-
  claw.out %>%
  dplyr::mutate(PartySummary = ifelse(PartySummary == "Neither", "Independent", PartySummary)) %>%
  ggplot() +
  geom_hline(yintercept=0, lty = "dashed", color = "grey") +
  geom_errorbar(aes(x=PartySummary,ymin=lower,ymax=upper),width=0) +
  geom_errorbar(aes(x=PartySummary,ymin=lb90,ymax=ub90), width=0, size = 1.75) +
  geom_point(aes(x=PartySummary,y=AME), size = 4) +
  facet_wrap(~model,scales = "free_y", nrow = 1) +
  scale_x_discrete(labels=scales::label_wrap(10)) +
  coord_flip() +
  labs(x="", y="Estimate", caption = "Notes: Thin and thick lines denote 95 and 90 percent confidence intervals") +
  book_theme
clawplot
#save book plot
savefig(clawplot,"8.6_clawback_nat", height = 1.8, filepath = "figures/")

## Create table for online appendix----
file <- "tables/ch8/ols_clawback_nat.txt"
modelsummary(
  list(claw, claw.pid, claw.trust, claw.green),
  vcov = "HC2",
  stars = c("*"=.1,"**"=.05,"***"=.01),
  coef_map = coefnames,
  gof_map = c("nobs", "adj.r.squared"),
  escape=FALSE,
  output = "latex"
) %>%
  cat(., file = file)
fix_txt(file)
